<?php
/**
 * Project: Recipe
 * File: RoleAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-2
 * Time: 上午9:04
 */


class RoleAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");
		$role = D('Role');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				$total = $role->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $role->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/Role/index');
			}
		}else{
			$total = $role->count();
			$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
			$show = $Page->show();// 分页显示输出
			$list = $role->relation(true)->limit($Page->firstRow.','.$Page->listRows)->order('id')->select();
		}
		
		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 指向添加分组界面
	 */
	function add(){
		$this->display();
	}

	/**
	 * 添加分组
	 */
	function insert(){
		$role = D('Role');
		$tmpdir = './Public/Content/tmp/';
		$srcdir = './Public/Content/Role/';
		if($data = $role->create()){
			if($role->add()){
				if(!empty($data['image'])){
					copy($tmpdir.$data['image'], $srcdir.$data['image']);
					if($dirsrc = opendir($tmpdir)){
						while($files = readdir($dirsrc)){
							if($files!='.' && $files!='..'){
								$pathfile = $tmpdir.$files;
								if(is_file($pathfile)){
									unlink($pathfile);
								}
							}
						}
					}
				}
				$this->success('添加分组成功', __APP__.'/Role/index');
			}else{
				$this->error('添加分组失败');
			}
		}else{
			$this->error($role->getError(), __APP__.'/Role/add');
		}
	}

	/**
	 *分组封面上传
	 */
	function uploads(){
		import("ORG.Net.emit_upload");
		import("ORG.Util.emit_image");
		$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
		if($upload->uploadfiles('pic')){
			$img = new image("./Public/Content/tmp");
			$imgname =  $upload->getnewname();
			if($img->cut($imgname)){
				$img->thumb($imgname, 70, 70);
			}
			echo $upload->getnewname();
		}else{
			echo $upload->geterrormsg();
		}
	}

	/**
	 * 指向修改分组界面
	 */
	function edit(){
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$role = M('Role');
			$data = $role->find($_GET['id']);
			$this->assign('data', $data);
			$this->display();
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 修改分组
	 */
	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$tmpdir = './Public/Content/tmp/';
			$srcdir = './Public/Content/Role/';
			$role = D('Role');
			$roleinfo = $role->relation(true)->find($_POST['id']);
			if($data = $role->create()){
				if($data['publish']==0){
					if(!empty($roleinfo['roleuser'])){
						$this->error('该分组下还有用户,不能关闭');
					}else{
						$role->save();
						if($data['image']!=$roleinfo['image']){
							unlink($srcdir.$roleinfo['image']);
							copy($tmpdir.$data['image'], $srcdir.$data['image']);
							if($dirsrc = opendir($tmpdir)){
								while($files = readdir($dirsrc)){
									if($files!='.' && $files!='..'){
										$pathfile = $tmpdir.$files;
										if(is_file($pathfile)){
											unlink($pathfile);
										}
									}
								}
							}
						}
						$this->success('修改分组成功',  __APP__.'/Role/index');
					}
				}else{
					$role->save();
						 if($data['image']!=$roleinfo['image']){
							 unlink($srcdir.$roleinfo['image']);
							 copy($tmpdir.$data['image'], $srcdir.$data['image']);
							 if($dirsrc = opendir($tmpdir)){
								 while($files = readdir($dirsrc)){
									 if($files!='.' && $files!='..'){
										 $pathfile = $tmpdir.$files;
										 if(is_file($pathfile)){
											 unlink($pathfile);
										 }
									 }
								 }
							 }
						 }
					$this->success('修改分组成功',  __APP__.'/Role/index');
				}
			}else{
				$this->error($role->getError());
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除分组
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$srcdir = './Public/Content/Role/';
			$role = D('Role');
			foreach($_POST['actions'] as $key=>$value){
				if($value==110){
					$this->error('此分组不能被删除');
				}else{
					$data = $role->relation(true)->find($value);
					if(empty($data['roleuser'])){
						$num = $role->delete($value);
						unlink($srcdir.$data['image']);
					}else{
						$this->error('该分组下还有用户');
					}
				}
			}
			if($num>0){
				$this->success('删除分组成功', __APP__.'/Role/index');
			}else{
				$this->error('删除分组失败');
			}
		}else{
			$this->error('请先选择分组');
		}
	}


}